package com.plian.system.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.plian.Tools.DateUtil;
import com.plian.Tools.TokenUtil;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

/**
 * @Description: 新增或更新自动填充字段
 * @Author husong
 * @Date 2023/2/9
 **/
@Component
public class AutoFillMetaObjectHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        this.setFieldValByName("createUser", TokenUtil.getTokenUserId(), metaObject);
        this.setFieldValByName("updateUser", TokenUtil.getTokenUserId(), metaObject);
        this.setFieldValByName("createTime", DateUtil.now(), metaObject);
        this.setFieldValByName("updateTime", DateUtil.now(), metaObject);
        this.setFieldValByName("createCompanyId", TokenUtil.getTokenOrgId(), metaObject);
        this.setFieldValByName("isDeleted", 0, metaObject);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        this.setFieldValByName("updateUser", TokenUtil.getTokenUserId(), metaObject);
        this.setFieldValByName("updateTime", DateUtil.now(), metaObject);
    }
}
